package org.voidness.squaretilesframework;

import org.voidness.squaretilesframework.sprites.GLSprite;

/** A class to represent a square tile */
public class Tile {
    /** The tile's associated tile image index in the tileset */
    private int imageIndex;

    /** The tile's associated object image index in the object set */
    private int objectIndex;

    /** Whether this tile is walkable by players or not */
    private boolean walkable;

    /** The sprite occupying this tile */
    private GLSprite occupier = null;

    /** The map where this tile belongs */
    private Map parent = null;

    /**
     * A tile constructor.
     * 
     * @param mImageIndex The image index (in the map tileset) for the tile
     * @param mObjectIndex The image index (in the map objectset) for the tile's
     *        object
     * @param map The map where the tile belongs to
     * @param mWalkable Whether this tile is walkable or not
     */
    public Tile(int mImageIndex, int mObjectIndex, Map mParent,
            boolean mWalkable) {
        imageIndex = mImageIndex;
        objectIndex = mObjectIndex;
        parent = mParent;
        walkable = mWalkable;
    }

    /**
     * Returns the tile's image index
     * 
     * @return The tile's image index in the map tileset
     */
    public int getImageIndex() {
        return imageIndex;
    }

    /**
     * (Re)defines the tile's image index
     * 
     * @param mImageIndex The index to set
     */
    public void setImageIndex(int mImageIndex) {
        imageIndex = mImageIndex;
    }

    /**
     * Returns the tile's object image index
     * 
     * @return The object image's index in the map object set
     */
    public int getObjectIndex() {
        return objectIndex;
    }

    /**
     * (Re)defines the object image's index
     * 
     * @param mObjectIndex The index to set
     */
    public void setObjectIndex(int mObjectIndex) {
        walkable = (mObjectIndex == -1) ? true : false;
        objectIndex = mObjectIndex;
    }

    /**
     * This method draws a tile in mX,mY.
     * 
     * @param mX The X position where to draw
     * @param mY The Y position where to draw
     * @param mAlpha The alpha value for the tile
     */
    public void draw(int mX, int mY, float mAlpha) {
        if (imageIndex > -1) {
            // parent.getTileSet().elementAt(imageIndex).setWidth(224); //
            // Temporary for Map Editor
            // parent.getTileSet().elementAt(imageIndex).setHeight(224); //
            // Temporary for Map Editor
            parent.getTileSet().elementAt(imageIndex).draw(mX, mY, mAlpha);
        }
    }

    /**
     * (Re)defines the tile's occupier
     * 
     * @param mOccupier The occupier to set
     */
    public void setOccupier(GLSprite mOccupier) {
        if (walkable && occupier == null)
            occupier = mOccupier;
    }

    /**
     * Returns the current tile occupier
     * 
     * @return The tile's GLSprite
     */
    public GLSprite getOccupier() {
        return occupier;
    }

    /**
     * Clears any occupation from this tile
     */
    public void clearOccupation() {
        occupier = null;
    }

    /**
     * Draws the tile occupier
     * 
     * @param mX The position to draw
     * @param mY The position to draw
     */
    public void drawOccupier(int mX, int mY) {
        if (occupier != null)
            occupier.draw(mX, mY);
    }

    /**
     * Draws the tile object
     * 
     * @param mX The position to draw
     * @param mY The position to draw
     */
    public void drawObject(int mX, int mY) {
        if (objectIndex > -1 && objectIndex < parent.getObjectTileSet().size())
            parent.getObjectTileSet().elementAt(objectIndex).draw(mX, mY);
    }

    /**
     * Returns whether this tile is walkable or not
     * 
     * @return True if so, false otherwise
     */
    public boolean isWalkable() {
        return walkable;
    }

    /**
     * Defines this tile as beeing walkable or not
     * 
     * @param mState True to set as walkable, false to set as not walkable
     */
    public void setWalkable(boolean mState) {
        walkable = mState;
    }

    /**
     * Returns whether this tile is occupied or not
     * 
     * @return True if it is, false otherwise
     */
    public boolean isOccupied() {
        return (occupier != null);
    }
}